package com.hz.monitoring.module.hardware.dal.mysql.mcu_tenant_type;

import cn.hutool.core.collection.CollectionUtil;
import com.hz.monitoring.framework.common.pojo.PageResult;
import com.hz.monitoring.framework.mybatis.core.mapper.BaseMapperX;
import com.hz.monitoring.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.hz.monitoring.module.hardware.controller.admin.mcu_tenant_type.vo.McuTenantTypeCreateReqVO;
import com.hz.monitoring.module.hardware.controller.admin.mcu_tenant_type.vo.McuTenantTypeExportReqVO;
import com.hz.monitoring.module.hardware.controller.admin.mcu_tenant_type.vo.McuTenantTypeListReqVO;
import com.hz.monitoring.module.hardware.controller.admin.mcu_tenant_type.vo.McuTenantTypePageReqVO;

import com.hz.monitoring.module.hardware.convert.mcu_tenant_ype.McuTenantTypeConvert;
import com.hz.monitoring.module.hardware.dal.dataobject.mcu_tenant_type.McuTenantTypeDO;

import org.apache.ibatis.annotations.Mapper;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * MCU租户分类 Mapper
 *
 * @author 芋道源码
 */
@Mapper
public interface McuTenantTypeMapper extends BaseMapperX<McuTenantTypeDO> {

    default PageResult<McuTenantTypeDO> selectPage(McuTenantTypePageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<McuTenantTypeDO>()
                .eqIfPresent(McuTenantTypeDO::getModel, reqVO.getModel())
                .likeIfPresent(McuTenantTypeDO::getName, reqVO.getName())
                .eqIfPresent(McuTenantTypeDO::getSpecification, reqVO.getSpecification())
                .eqIfPresent(McuTenantTypeDO::getVendor, reqVO.getVendor())
                .eqIfPresent(McuTenantTypeDO::getEdition, reqVO.getEdition())
                .betweenIfPresent(McuTenantTypeDO::getCreateTime, reqVO.getBeginCreateTime(), reqVO.getEndCreateTime())
                .orderByDesc(McuTenantTypeDO::getId));
    }


    default Boolean createBatchMcuTenantType(List<McuTenantTypeCreateReqVO> createReqVOList) {
        List<McuTenantTypeDO> list = selectList(new LambdaQueryWrapperX<McuTenantTypeDO>().eq(McuTenantTypeDO::getTenantId, createReqVOList.get(0).getTenantId()));

        Map<String, McuTenantTypeCreateReqVO> mcuSysTypeIdByData = createReqVOList.stream().collect(Collectors.toMap(McuTenantTypeCreateReqVO::getModel, a -> a, (k1, k2) -> k1));
        Map<String, McuTenantTypeDO> mcuSysTypeIdByMcuTenantTypeDO = list.stream().collect(Collectors.toMap(McuTenantTypeDO::getModel, a -> a, (k1, k2) -> k1));
        //获取要删除的数据
        List<Long> deleteIds = list.stream().filter(it -> !mcuSysTypeIdByData.containsKey(it.getModel())).map(it -> it.getId()).collect(Collectors.toList());
        if (CollectionUtil.isNotEmpty(deleteIds)) {
            deleteBatchIds(deleteIds);
        }
        //获取要添加的数据
        createReqVOList=  createReqVOList.stream().filter(it -> !mcuSysTypeIdByMcuTenantTypeDO.containsKey(it.getModel())).collect(Collectors.toList());
        if (CollectionUtil.isNotEmpty(createReqVOList)) {
            List<McuTenantTypeDO> mcuTenantTypeList = McuTenantTypeConvert.INSTANCE.convert(createReqVOList);
            insertBatch(mcuTenantTypeList);
        }
        return true;
    }

    default List<McuTenantTypeDO> selectList(McuTenantTypeListReqVO reqVO) {
        return selectList(new LambdaQueryWrapperX<McuTenantTypeDO>()
                .eqIfPresent(McuTenantTypeDO::getModel, reqVO.getModel())
                .likeIfPresent(McuTenantTypeDO::getName, reqVO.getName())
                .eqIfPresent(McuTenantTypeDO::getSpecification, reqVO.getSpecification())
                .eqIfPresent(McuTenantTypeDO::getVendor, reqVO.getVendor())
                .eqIfPresent(McuTenantTypeDO::getEdition, reqVO.getEdition())
                .eqIfPresent(McuTenantTypeDO::getTenantId,reqVO.getTenantId())
                .inIfPresent(McuTenantTypeDO::getTenantId,reqVO.getIds())
                .betweenIfPresent(McuTenantTypeDO::getCreateTime, reqVO.getBeginCreateTime(), reqVO.getEndCreateTime())
                .orderByDesc(McuTenantTypeDO::getId));
    }

    default List<McuTenantTypeDO> selectList(McuTenantTypeExportReqVO reqVO) {
        return selectList(new LambdaQueryWrapperX<McuTenantTypeDO>()
                .eqIfPresent(McuTenantTypeDO::getModel, reqVO.getModel())
                .likeIfPresent(McuTenantTypeDO::getName, reqVO.getName())
                .eqIfPresent(McuTenantTypeDO::getSpecification, reqVO.getSpecification())
                .eqIfPresent(McuTenantTypeDO::getVendor, reqVO.getVendor())
                .eqIfPresent(McuTenantTypeDO::getEdition, reqVO.getEdition())
                .betweenIfPresent(McuTenantTypeDO::getCreateTime, reqVO.getBeginCreateTime(), reqVO.getEndCreateTime())
                .orderByDesc(McuTenantTypeDO::getId));
    }

}
